package com.lxw.nonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.annotation.RequiresPermission;
import com.lxw.nonghe.common.Result;
import com.lxw.nonghe.entity.Permission;
import com.lxw.nonghe.service.PermissionService;
import com.lxw.nonghe.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@RequiresPermission("permission:manage")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private LogUtils logUtils;

    @PostMapping("/create")
    public Result<Boolean> addPermission(@RequestBody Permission permission) {
        try {
            // 检查权限名称是否已存在
            if (permissionService.getPermissionByPermissionName(permission.getPermissionName()) != null) {
                logUtils.recordLog("权限管理", "新增权限", "失败", "权限名称已存在:" + permission.getPermissionName());
                return Result.error("该权限名称已存在，请重新编写权限名称");
            }

            boolean result = permissionService.save(permission);
            if (result) {
                logUtils.recordLog("权限管理", "新增权限", "成功",
                        "权限ID:" + permission.getPermissionId() + ", 名称:" + permission.getPermissionName());
                return Result.success("权限添加成功", true);
            } else {
                logUtils.recordLog("权限管理", "新增权限", "失败", "保存数据库失败");
                return Result.error("权限添加失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("权限管理", "新增权限", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/delete/{id}")
    public Result<Boolean> deletePermission(@PathVariable Integer id) {
        try {
            Permission permission = permissionService.getById(id);
            boolean result = permissionService.removeById(id);

            if (result) {
                logUtils.recordLog("权限管理", "删除权限", "成功",
                        "权限ID:" + id + ", 名称:" + (permission != null ? permission.getPermissionName() : "未知"));
                return Result.success("权限删除成功", true);
            } else {
                logUtils.recordLog("权限管理", "删除权限", "失败", "权限ID:" + id + "不存在");
                return Result.error("权限删除失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("权限管理", "删除权限", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> updatePermission(@RequestBody Permission permission) {
        try {
            // 检查权限名称是否已被其他权限使用
            Permission existingPermission = permissionService.getPermissionByPermissionName(permission.getPermissionName());
            if (existingPermission != null && !existingPermission.getPermissionId().equals(permission.getPermissionId())) {
                logUtils.recordLog("权限管理", "更新权限", "失败", "权限名称冲突:" + permission.getPermissionName());
                return Result.error("该权限名称已存在，请重新编写");
            }

            boolean result = permissionService.updateById(permission);
            if (result) {
                logUtils.recordLog("权限管理", "更新权限", "成功",
                        "权限ID:" + permission.getPermissionId());
                return Result.success("权限修改成功", true);
            } else {
                logUtils.recordLog("权限管理", "更新权限", "失败",
                        "权限ID:" + permission.getPermissionId() + "不存在");
                return Result.error("权限修改失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("权限管理", "更新权限", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @GetMapping("/get/{id}")
    public Result<Permission> getPermissionById(@PathVariable Integer id) {
        try {
            Permission permission = permissionService.getById(id);
            if (permission != null) {
                /*logUtils.recordLog("权限管理", "查询权限详情", "成功", "权限ID:" + id);*/
                return Result.success("权限查询成功", permission);
            } else {
                logUtils.recordLog("权限管理", "查询权限详情", "失败", "权限ID:" + id + "不存在");
                return Result.error("权限查询失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("权限管理", "查询权限详情", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<Page<Permission>> getPermissionPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String permissionName) {
        try {
            Page<Permission> permissionPage = permissionService.getPermissionPage(current, size, permissionName);
            /*logUtils.recordLog("权限管理", "分页查询权限", "成功",
                    "页码:" + current + ", 大小:" + size + ", 权限名称:" + permissionName);*/
            return Result.success("权限分页查询成功", permissionPage);
        } catch (Exception e) {
            logUtils.recordLog("权限管理", "分页查询权限", "失败", e.getMessage());
            return Result.error("分页查询异常: " + e.getMessage());
        }
    }

    @PostMapping("/query")
    public Result<Page<Permission>> queryPermission(
            @RequestBody Permission permission,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Page<Permission> permissionPage = permissionService.queryPermission(permission, current, size);
            /*logUtils.recordLog("权限管理", "条件查询权限", "成功",
                    "条件:" + permission.toString() + ", 页码:" + current + ", 大小:" + size);*/
            return Result.success("权限条件查询成功", permissionPage);
        } catch (Exception e) {
            logUtils.recordLog("权限管理", "条件查询权限", "失败", e.getMessage());
            return Result.error("条件查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/tree")
    public Result<List<Permission>> getPermissionTree() {
        try {
            List<Permission> permissionTree = permissionService.getPermissionTree();
            /*logUtils.recordLog("权限管理", "获取权限树", "成功",
                    "权限树节点数:" + permissionTree.size());*/
            return Result.success(permissionTree);
        } catch (Exception e) {
            logUtils.recordLog("权限管理", "获取权限树", "失败", e.getMessage());
            return Result.error("获取权限树异常: " + e.getMessage());
        }
    }
}
